﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;

namespace TextFlow.Activities
{
    public abstract class ActionOutputActivity : Activity
    {
        public ActionOutputActivity()
        {
        }

        public ActionOutputActivity( string name ) : base( name )
        {
        }

        public string BoundPropertyName { get; set; }
        public string VariableName { get; set; }

        protected override ActivityExecutionStatus Execute( ActivityExecutionContext aec )
        {
            Debug.Assert( string.IsNullOrEmpty( VariableName ) == false );
            Debug.Assert( string.IsNullOrEmpty( BoundPropertyName ) == false );

            PropertyInfo prop = this.GetType().GetProperty( BoundPropertyName );

            Debug.Assert( prop != null );

            object value = prop.GetValue( this, null );

            TextFlowWorkflow root = this.GetActivityByName( "MainFlow", false ) as TextFlowWorkflow;

            Debug.Assert( root != null );

            root.Operands[ VariableName ] = value;

            return ActivityExecutionStatus.Closed;
        }
    }
}
